const fs = require("fs");
const path = require("path");
const { accept } = require("../constant/accept.js");

function round() {
  return Math.random().toString(16).substr(2, 6);
}

function checkInputFilePath(filePath) {
  if (!fs.existsSync(path.resolve(filePath))) return false;

  const [extension] = getFilenameAndExtByPath(filePath);

  if (!accept.includes(extension)) return false;

  return true;
}

function getFilenameAndExtByPath(filePath) {
  const index = filePath.lastIndexOf(".");
  const extension = filePath.substring(index + 1, filePath.length); //index是点的位置。点的位置加1再到结尾
  const name = filePath.substring(0, index);

  return ["." + extension, name];
}

function transformObjectKeys(obj, prefix) {
  const _obj = {};

  for (const key in obj) {
    _obj[prefix + "." + key] = obj[key];
  }
  
  return _obj;
}

module.exports = {
  round,
  getFilenameAndExtByPath,
  checkInputFilePath,
  transformObjectKeys,
};
